from django.contrib.auth.backends import ModelBackend
from users.models import User
from django.db.models import Q


class CustomAuthenticate(ModelBackend):

    def authenticate(self, request, username=None, password=None, **kwargs):

        try:
            user = User.objects.get(
                Q(username=username) | Q(mobile=username)
            )
        except User.DoesNotExist as e:
            return None

        if user.check_password(password):
            return user